import { app, ipcMain } from 'electron'
import Store from 'electron-store'
import { IpcEvent } from 'app/type'

export class ElectronStore {
    static store = new Store({ name: 'userStore' })

    static init() {
        const handlerMap = new Map<IpcEvent, Function>()
        handlerMap.set(IpcEvent.STORE_GET, ElectronStore.store.get)
        handlerMap.set(IpcEvent.STORE_SET, ElectronStore.store.set)
        handlerMap.set(IpcEvent.STORE_HAS, ElectronStore.store.has)
        handlerMap.set(IpcEvent.STORE_CLEAR, ElectronStore.store.clear)
        handlerMap.set(IpcEvent.STORE_RESET, ElectronStore.store.reset)
        handlerMap.set(IpcEvent.STORE_DELETE, ElectronStore.store.delete)
        handlerMap.set(IpcEvent.STORE_OPEN_IN_EDITOR, ElectronStore.store.openInEditor)
        handlerMap.forEach((handler, channel) => {
            ipcMain.handle(channel, (e, ...args) => {
                return (handler as Function).call(ElectronStore.store, ...args)
            })
        })
    }
}
